package com.example.health_hjk.food_list;

import android.content.Intent;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.health_hjk.R;
import com.example.health_hjk.bean.FoodBean;
import com.example.health_hjk.bean.FoodUtils;
import com.example.health_hjk.food_grid.FoodDescActivity;

import java.util.ArrayList;
import java.util.List;

public class InfoListActivity extends AppCompatActivity implements View.OnClickListener{
    EditText searchEt;
    ImageView searchIv,flushIv;
    ListView showLv;
    //ListView内部数据源
    List<FoodBean>mDatas;
    //用一个中间变量储存数据，就不用每次都重新调用了。
    List<FoodBean>allFoodList;
    private InfolistAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_list);
        //查找控件
        initView();
        //找到ListView对应的数据源
        mDatas = new ArrayList<>();
        //在Utils调用方法
        allFoodList = FoodUtils.getAllFoodList();
        mDatas.addAll(allFoodList);
        //创建适配器 BaseAdapter的子类
        adapter = new InfolistAdapter(this,mDatas);
        showLv.setAdapter(adapter);
        //设置单项监听功能
        setListener();
    }

    private void setListener() {
        showLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                FoodBean foodBean = mDatas.get(position);
                Intent intent = new Intent(InfoListActivity.this, FoodDescActivity.class);
                intent.putExtra("food",foodBean);
                startActivity(intent);
            }
        });
    }

    //查找控件
    private void initView() {
        searchEt = findViewById(R.id.info_et_search);
        searchIv = findViewById(R.id.info_iv_search);
        flushIv = findViewById(R.id.info_iv_flush);
        showLv = findViewById(R.id.infolist_lv);
        //添加点击事件
        searchIv.setOnClickListener(this);
        flushIv.setOnClickListener(this);
    }
    //创建点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.info_iv_flush: //刷新点击
                mDatas.clear();
                mDatas.addAll(allFoodList);
                adapter.notifyDataSetChanged();
                break;
                //点击了搜索按钮以后的操作
            case R.id.info_iv_search: //搜索点击
                //获取输入内容，判断不为空
                String msg = searchEt.getText().toString().trim();
                if(TextUtils.isEmpty(msg)){
                    Toast.makeText(this,"输入内容不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                //将输入的信息拿到整个数据库中进行判断
                List<FoodBean>list = new ArrayList<>();
                for(int i = 0;i < allFoodList.size();i++){
                    String title = allFoodList.get(i).getTitle();
                    if(title.contains(msg)){
                        list.add(allFoodList.get(i));
                    }
                }
                //清空原来的数据源内容
                mDatas.clear();
                //添加新的数据源
                mDatas.addAll(list);
                //提示适配器更新
                adapter.notifyDataSetChanged();
                break;

        }
    }
}
